<template>
  <div>
    <!-- 头部 -->
    <div class="dark-top">
      <div class="page-title">
        <font style="vertical-align: inherit;">广场</font>
      </div>
    </div>
    <!-- 内容 -->
    <div class="dark-body">
      <mt-loadmore
        v-infinite-scroll="loadMore"
        infinite-scroll-disabled="busy"
        infinite-scroll-distance="10"
        infinite-scroll-immediate-check="true"
      >
        <div v-for="(item, index) in squareList" :key="item.id">
          <div class="dark-list">
            <!-- 列表头 -->
            <div class="content-top">
              <div class="top-head">
                <div
                  :style="{ backgroundImage: 'url(' + item.head + ')' }"
                ></div>
              </div>
              <div class="top-right">
                <div>
                  <span class="top-name">{{ item.nickname }}</span>
                  <span
                    class="top-del"
                    @click="deleteSquare(item.id)"
                    v-if="item.delete == 1"
                  ></span>
                </div>
                <div>
                  <span class="top-class">{{ item.class }}班</span>
                  <span class="top-audit">{{ item.status }}</span>
                </div>
              </div>
            </div>
            <!-- 列表文本 -->
            <div class="cont-text" @click="goSquareDetails(item.id)">
              <div class="text-content">
                {{ item.graphic }}
                <span class="span-show">展开</span>
              </div>
              <div class="text-img">
                <div
                  class="text-img-back"
                  :style="{ backgroundImage: 'url(' + item.picture + ')' }"
                ></div>
                <div class="text-img-float">
                  <div></div>
                  <span>共{{ item.picture_num }}张</span>
                </div>
              </div>
            </div>
            <!-- 列表点赞 -->
            <div class="cont-button">
              <div
                class="button-div3"
                @click="giveLike(2, index)"
                v-if="item.state"
              >
                <div></div>
                <span>{{ item.give }}</span>
              </div>
              <div class="button-div1" @click="giveLike(1, index)" v-else>
                <div></div>
                <span>{{ item.give }}</span>
              </div>
              <div class="button-div2">
                <div></div>
                <span>{{ item.share }}</span>
              </div>
            </div>
          </div>
          <div class="list-hr"></div>
        </div>
        <p v-show="infiniteMsgShow" class="tips">加载下一页</p>
        <p v-show="!infiniteMsgShow" class="tips">没有更多数据</p>
      </mt-loadmore>
      <div style="height:4.375rem;"></div>
    </div>

    <!-- 发布图标 -->
    <div class="cart" @click="goSquareRelease"><div></div></div>
  </div>
</template>

<script>
import { Indicator } from "mint-ui";
import { Toast } from "mint-ui";
import { MessageBox } from "mint-ui";
export default {
  data: function() {
    return {
      busy: true,
      page: 0,
      limit: 10,
      contentShow: "",
      infiniteMsgShow: true,
      squareList: [],
      allLoaded: true,
      giveClass: "button-div1"
    };
  },
  mounted: function() {},
  created() {
    this.loadMore();
  },
  methods: {
    // 当属性滚动的时候  加载  滚动加载
    loadMore() {
      this.busy = true; // 将无限滚动给禁用
      setTimeout(() => {
        //发送请求有时间间隔第一个滚动时间结束后才发送第二个请求
        this.page++; //滚动之后加载第二页
        this.selectSquare();
      }, 500);
    },
    //点赞
    giveLike(val, i) {
      Indicator.open({ spinnerType: "fading-circle" });
      //console.log(this.squareList[i].state);
      if (val == 1) {
        this.squareList[i].state = true;
      }
      if (val == 2) {
        this.squareList[i].state = false;
      }
      let me = this;
      this.$axios
        .post(
          "/api/square/squareGiveLike",
          { uid: localStorage.uid, ft_id: this.squareList[i].id, state: val },
          { emulateJSON: true }
        )
        .then(kun => {
          //console.log(kun);
          Indicator.close();
          if (kun.code == 200) {
            me.squareList[i].give = kun.data;
          } else {
            Toast(kun.msg);
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    goSquareDetails(val) {
      localStorage.ft_id = val;
      //到详情页
      this.$router.push({
        name: "SquareDetails",
        params: { ft_id: val }
      });
    },
    goSquareRelease() {
      //到发帖页 只有助教能够发帖
      Indicator.open({ spinnerType: "fading-circle" });
      let me = this;
      this.$axios
        .post(
          "/api/square/squareTeach",
          { uid: localStorage.uid },
          { emulateJSON: true }
        )
        .then(kun => {
          console.log(kun);
          Indicator.close();
          if (kun.code == 200) {
            me.$router.push({
              name: "Release"
            });
          } else {
            Toast(kun.msg);
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    //获取发帖列表
    selectSquare() {
      Indicator.open({ spinnerType: "fading-circle" });
      let me = this;
      this.$axios
        .post(
          "/api/square/index",
          { uid: localStorage.uid, page: this.page, limit: this.limit },
          { emulateJSON: true }
        )
        .then(kun => {
          console.log(kun);
          Indicator.close();

          let listTotal = kun.total;
          if (listTotal > 0) {
            me.contentShow = true;
          } else {
            me.contentShow = false;
          }
          me.squareList = me.squareList.concat(kun.data);
          let DownloadTotal = me.page * me.limit;
          let total = kun.total;

          if (DownloadTotal > total) {
            me.busy = true; // 将无限滚动关闭
            me.page = 0; // 页数变为0
            me.infiniteMsgShow = false; // 切换底部提示信息
          } else {
            me.busy = false; // 当还有多余的数据时，将无限滚动给打开 ，就是可以继续滚动去请求后台
            me.infiniteMsgShow = true;
          }

          if (kun.code != 200) {
            Toast(kun.msg);
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    deleteSquare(val) {
      //删除发帖内容
      let me = this;
      MessageBox.confirm("确定执行此操作?").then(function() {
        me.$axios
          .post(
            "/api/square/squareDelete",
            {
              uid: localStorage.uid,
              ft_id: val
            },
            { emulateJSON: true }
          )
          .then(kun => {
            console.log(kun);
            if (kun.code != 200) {
              Toast(kun.msg);
            }
          })
          .catch(function(error) {
            console.log(error);
          });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
//下拉 没有数据
.empty {
  font-size: 0.875rem;
  color: #999999;
  text-align: center;
  margin-top: 5.25rem;
}
//下拉 加载下一页
.tips {
  font-size: 0.875rem;
  color: #999999;
  text-align: center;
  margin: 1.25rem 0;
}
//头部
.dark-top {
  width: 100%;
  height: 2.75rem;
  color: #1a1a1a;
  position: relative;

  /* 设置渐变 */
  background: rgb(255, 0, 0); /* Old browsers */
  background: -moz-linear-gradient(
    to right bottom,
    rgb(255, 127, 95) 10%,
    rgb(229, 39, 77) 70%
  ); /* FF3.6-15 */
  background: -webkit-linear-gradient(
    to right bottom,
    rgb(255, 127, 95) 10%,
    rgb(229, 39, 77) 70%
  ); /* Chrome10-25,Safari5.1-6 */
  background: linear-gradient(
    to right bottom,
    rgb(255, 127, 95) 10%,
    rgb(229, 39, 77) 70%
  ); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ff0000', endColorstr='#ffffff',GradientType=0 ); /* IE6-9 */

  .page-title {
    font-size: 1.125rem;
    color: #ffffff;
    display: inline-block;
    position: absolute;
    left: 50%;
    top: 0.65rem;
    -ms-transform: translateX(-50%) translateY(0); /* IE 9 */
    -moz-transform: translateX(-50%) translateY(0); /* Firefox */
    -webkit-transform: translateX(-50%) translateY(0); /* Safari 和 Chrome */
    -o-transform: translateX(-50%) translateY(0); /* Opera */
  }
}
//内容
.dark-body {
  //列表
  .dark-list {
    width: 21.5rem;
    margin: 1rem auto 0;
    //列表头部信息
    .content-top {
      height: 2.75rem;

      .top-head {
        width: 2.75rem;
        height: 2.75rem;
        border-radius: 1.375rem;
        float: left;

        :first-child {
          width: 100%;
          height: 100%;
          border-radius: 1.375rem;
          background-image: url(../../assets/login/myHead.png);
          background-repeat: no-repeat;
          background-size: cover;
          background-position: center;
          overflow: hidden;
        }
      }

      .top-right {
        width: 18.125rem;
        float: right;

        .top-name {
          font-size: 0.9375rem;
          color: #333333;
          width: 89%;
          overflow: hidden;
          display: inline-block;
        }
        .top-del {
          width: 0.9375rem;
          height: 0.9375rem;
          display: inline-block;
          float: right;
          background-image: url(../../assets/login/text-del.png);
          background-repeat: no-repeat;
          background-size: contain;
          background-position: center;
          overflow: hidden;
        }
        .top-class {
          font-size: 0.75rem;
          color: #999999;
          float: left;
        }
        .top-audit {
          font-size: 0.75rem;
          color: #ff0000;
          float: right;
        }
      }
    }
    // 列表文本
    .cont-text {
      width: 18.125rem;
      margin-top: 0.5625rem;
      float: right;

      .text-content {
        font-size: 0.9375rem;
        color: #1a1a1a;
        overflow: hidden;
        max-height: 6.125rem;
        width: 18.125rem;
        position: relative;
        .span-show {
          color: #3eadeb;
        }
      }
      .text-img {
        width: 100%;
        height: 10.1875rem;
        margin-top: 1rem;
        position: relative;

        .text-img-back {
          width: 100%;
          height: 100%;
          background-image: url(../../assets/login/content.png);
          background-repeat: no-repeat;
          background-size: cover;
          background-position: center;
          overflow: hidden;
        }

        .text-img-float {
          position: absolute;
          bottom: 0.375rem;
          left: 0.375rem;
          height: 1.125rem;
          background-color: #000000;
          opacity: 0.5;
          color: #ffffff;
          font-size: 0.625rem;

          :first-child {
            margin: 0.3125rem 0.3125rem 0 0.3125rem;
            width: 0.625rem;
            height: 0.5625rem;
            float: left;
            background-image: url(../../assets/login/xc-img.png);
            background-repeat: no-repeat;
            background-size: contain;
            background-position: center;
            overflow: hidden;
          }
          span {
            float: left;
            display: inline-block;
          }
        }
      }
    }
    // 列表点赞
    .cont-button {
      width: 18.125rem;
      height: 0.9375rem;
      margin-top: 1.0625rem;
      float: right;
      font-size: 0.75rem;
      color: #343434;

      .button-div1 {
        display: inline-block;
        div {
          width: 0.9375rem;
          height: 0.9375rem;
          display: inline-block;
          background-image: url(../../assets/login/dian-zan.png);
          background-repeat: no-repeat;
          background-size: contain;
          background-position: center;
          overflow: hidden;
        }
        span {
          display: inline-block;
          margin-left: 0.625rem;
          line-height: 0.9375rem;
        }
      }
      .button-div2 {
        display: none;
        margin-left: 2.5rem;
        div {
          width: 0.9375rem;
          height: 0.9375rem;
          display: inline-block;
          background-image: url(../../assets/login/fen-xiang.png);
          background-repeat: no-repeat;
          background-size: contain;
          background-position: center;
          overflow: hidden;
        }
        span {
          display: inline-block;
          margin-left: 0.625rem;
          line-height: 0.9375rem;
        }
      }
      .button-div3 {
        display: inline-block;

        div {
          width: 0.9375rem;
          height: 0.9375rem;
          display: inline-block;
          background-image: url(../../assets/login/dian-zanred.png);
          background-repeat: no-repeat;
          background-size: contain;
          background-position: center;
          overflow: hidden;
        }
        span {
          display: inline-block;
          margin-left: 0.625rem;
          line-height: 0.9375rem;
        }
      }
    }
  }
  .list-hr {
    width: 100%;
    height: 1.25rem;
    clear: both;
    border-bottom: 1px solid #e6e6e6;
  }
}
//购物车
.cart {
  position: fixed;
  bottom: 4.6875rem;
  right: 0.75rem;
  :first-child {
    width: 3.75rem;
    height: 3.75rem;
    background-image: url(../../assets/login/share.png);
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center;
    overflow: hidden;
  }
}
</style>
